package net.asfun.jvalog.mvc;

import static net.asfun.jvalog.common.log.logger;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class MappingAnnotationResolver {

	public static Map<String, HandlerController> search(Object controller) throws Exception {
		Map<String, HandlerController> mappings = new HashMap<String, HandlerController>();
		Class<?> clazz = controller.getClass();
		Method[] methods = clazz.getDeclaredMethods();
		for ( Method method : methods ) {
			RequestMapping anno = method.getAnnotation(RequestMapping.class);
			if ( anno == null ) continue;
			String[] uris = anno.value();
			HandlerController hc = new HandlerController(controller, method);
			for(String uri : uris) {
				mappings.put(uri.trim(), hc);
				logger.info("\tMapping \"" + uri.trim() + "\" to " + hc);
			}
		}
		return mappings;
	}
}
